/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bsc.war.populate;

import bsc.ejb.entity.*;
import bsc.ejb.metadata.Colorimetry;
import bsc.ejb.metadata.Comparation;
import bsc.ejb.metadata.Framework;
import bsc.ejb.metadata.MapDrawing;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author luisZavaleta
 */
public class SettingsPopulate2 extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");

        //Create a Framework
        Framework framework = (Framework) Factory.getBean("Framework");
        framework.setName("Test");
        framework.setActive(Boolean.TRUE);
        delegate.createFramework(framework);

        //Create Map Drawing
        MapDrawing mapDrawing = (MapDrawing) Factory.getBean("MapDrawing");
        mapDrawing.setName("Dibujo del Mapa de Néctares del Sureste");
        mapDrawing.setDescription("Mapa");
        mapDrawing.setFile("http://lamanchamagenta.com/lol/MapaNectares.jpg");
        mapDrawing.setFramework(framework);
        delegate.createMapDrawing(mapDrawing);

        //Map
        Map map = (Map)delegate.findScoreCard(1L);
        map.setMapDrawing(mapDrawing);
        delegate.updateMap(map);
        
        mapDrawing.setMap(map);
        delegate.updateMapDrawing(mapDrawing);

        
        

        //Create a Colorimetries
        //Green
        Colorimetry colorimetryGreen = (Colorimetry) Factory.getBean("Colorimetry");
        colorimetryGreen.setName("Green");
        colorimetryGreen.setPriority(1);
        colorimetryGreen.setFramework(framework);
        colorimetryGreen.setColor("00FF00");
        delegate.createColorimetry(colorimetryGreen);

        //Yellow
        Colorimetry colorimetryYellow = (Colorimetry) Factory.getBean("Colorimetry");
        colorimetryYellow.setName("Yellow");
        colorimetryYellow.setPriority(2);
        colorimetryYellow.setFramework(framework);
        colorimetryYellow.setColor("FFFF00");
        delegate.createColorimetry(colorimetryYellow);


        //Red
        Colorimetry colorimetryRed = (Colorimetry) Factory.getBean("Colorimetry");
        colorimetryRed.setName("Red");
        colorimetryRed.setPriority(3);
        colorimetryRed.setFramework(framework);
        colorimetryRed.setColor("FF0000");
        delegate.createColorimetry(colorimetryRed);


        //Settings Scorecards
        for (long i = 1; i <= 6; i++) {

            ScoreCard scoreCard = delegate.findScoreCard(i);

            if (!(scoreCard instanceof Pointer)) {
                Settings setting = (Settings) Factory.getBean("Settings");
                setting.setType(Settings.Type.SCORECARD);
                setting.setScoreCard(scoreCard);
                delegate.createSettings(setting);

                scoreCard.setSettings(setting);
                delegate.updateScoreCard(scoreCard);
            }
        }





        //Find Comparation type REAL
        Comparation comparation = delegate.findComparation(1L);
        Comparation comparation2 = delegate.findComparation(2L);

        comparation.setFramework(framework);
        comparation2.setFramework(framework);

        delegate.updateComparation(comparation);
        delegate.updateComparation(comparation2);


        //Find the scorecard and dimensionalCut
        Pointer pointer = (Pointer) delegate.findScoreCard(4L);
        Comparator comparator = delegate.findComparator(pointer, comparation);
        Long idComparator = comparator.getIdComparator();

        //Create setting to that pointer
        Settings setting = (Settings) Factory.getBean("Settings");
        setting.setType(Settings.Type.SCORECARD);
        setting.setTemporality(Settings.Temporality.X_YEARLY);
        setting.setMathOperation("C" + idComparator + "/P");
        setting.setScoreCard(pointer);
        delegate.createSettings(setting);

        pointer.setSettings(setting);
        delegate.updatePointer(pointer);
        //Get Comparator of the pointer

        List<Comparator> comparators = delegate.findComparator(pointer);
        for (Comparator comparator1 : comparators) {
            Settings settingComparator = (Settings) Factory.getBean("Settings");
            settingComparator.setType(Settings.Type.COMPARATOR);
            settingComparator.setCommand(Settings.Command.SUM);
            settingComparator.setComparator(comparator1);
            delegate.createSettings(settingComparator);

            comparator1.setSettings(settingComparator);
            delegate.updateComparator(comparator1);
        }


        //Find the scorecard and dimensionalCut
        Pointer pointer2 = (Pointer) delegate.findScoreCard(5L);
        Comparator comparator2 = delegate.findComparator(pointer2, comparation);
        Long idComparator2 = comparator2.getIdComparator();

        //Create setting to that pointer
        Settings setting2 = (Settings) Factory.getBean("Settings");
        setting2.setType(Settings.Type.SCORECARD);
        setting2.setTemporality(Settings.Temporality.LAST);
        setting2.setMathOperation("P/" + "C" + idComparator2);
        setting2.setScoreCard(pointer2);
        delegate.createSettings(setting2);

        pointer2.setSettings(setting2);
        delegate.updatePointer(pointer2);
        //Get Comparator of the pointer

        List<Comparator> comparators2 = delegate.findComparator(pointer2);
        for (Comparator comparator1 : comparators2) {
            Settings settingComparator = (Settings) Factory.getBean("Settings");
            settingComparator.setType(Settings.Type.COMPARATOR);
            settingComparator.setCommand(Settings.Command.SUM);
            settingComparator.setComparator(comparator1);
            delegate.createSettings(settingComparator);

            comparator1.setSettings(settingComparator);
            delegate.updateComparator(comparator1);
        }


        //Rangos
        Double arrayd[]= {10000D, .9D, .8D, -100000D};
        for (long i = 1; i <= 6; i++) {
            ScoreCard scoreCard = delegate.findScoreCard(i);
            Settings settingSC = scoreCard.getSettings();
            int cont=1;
            for (long j = 1; j <= 3; j++) {
                Colorimetry colorimetry = delegate.findColorimetry(j);
                ColorimetrySettings cs = (ColorimetrySettings) Factory.getBean("ColorimetrySettings");
                cs.setSettings(settingSC);
                cs.setColorimetry(colorimetry);
                cs.setMaxScore(arrayd[cont-1]);
                cs.setMinScore(arrayd[cont]);
                delegate.createColorimetrySettings(cs);
                cont++;
            }

        }

        //MapDrawingSettings
        //Mapa
        ScoreCard scoreMap= delegate.findScoreCard(1L);
        MapDrawingSettings mps = (MapDrawingSettings) Factory.getBean("MapDrawingSettings");
        mps.setSettings(scoreMap.getSettings());
        mps.setMapDrawing(mapDrawing);
        mps.setX(575D);
        mps.setY(75D);
        delegate.createMapDrawingSettings(mps);


        //Perspectiva
        ScoreCard scorePer= delegate.findScoreCard(2L);
        MapDrawingSettings mps2 = (MapDrawingSettings) Factory.getBean("MapDrawingSettings");
        mps2.setSettings(scorePer.getSettings());
        mps2.setMapDrawing(mapDrawing);
        mps2.setX(292D);
        mps2.setY(150D);
        delegate.createMapDrawingSettings(mps2);


        //Proyecto
        ScoreCard scorePro= delegate.findScoreCard(6L);
        MapDrawingSettings mps3 = (MapDrawingSettings) Factory.getBean("MapDrawingSettings");
        mps3.setSettings(scorePro.getSettings());
        mps3.setMapDrawing(mapDrawing);
        mps3.setX(850D);
        mps3.setY(150D);
        delegate.createMapDrawingSettings(mps3);

        return new ModelAndView("advancePopulate");
    }
}
